import com.im30.jenkins.pipeline.utils.BuildConfig

def call() {
    // This limits build concurrency to 1 per branch
    properties([
            disableConcurrentBuilds(),
            buildDiscarder(logRotator(artifactDaysToim30Str: '', artifactNumToim30Str: '', daysToim30Str: '7', numToim30Str: '50'))
    ])

    dockerNode.nodeBuildDocker {
        BuildConfig buildConfig
        boolean isSuccess = false
        try {
            stage('Preparation') {
                // fetch remote tags
                checkout scm: [
                        $class: 'GitSCM',
                        branches: scm.branches,
                        doGenerateSubmoduleConfigurations: scm.doGenerateSubmoduleConfigurations,
                        extensions: [[$class: 'CloneOption', noTags: false, shallow: false, depth: 0, reference: '']],
                        userRemoteConfigs: scm.userRemoteConfigs
                ]
            }

            buildConfig = BuildConfig.load(this)
            if (buildConfig == null) {
                throw new Exception("Cannot parse build.yaml")
            }

            List<Object> appInfos = buildConfig.getApps()

            stage('Sonar') {
                echo 'Sonar Repo'
                for (Object app : appInfos) {
                    echo "Try Sonar: ${app.name}"
                    if (app.sonar == 'Need') {
                        // npmUtil.sonar(jobUtil.getRepoName())
                    }
                }
            }

            stage('lint') {
                echo 'Lint Repo'
                for (Object app : appInfos) {
                    echo "Try Lint: ${app.name}"
                    if (app.lint == 'Need') {
                        npmUtil.lint(jobUtil.getRepoName())
                    }
                }
            }

            stage('Security Scan') {
                parallel (
                        "murphyScan": {
                            stage('murphyScan') {
                                try {
                                    securityUtil.murphyScan(jobUtil.getRepoName())
                                } catch (err) {
                                    echo err.getMessage()
                                }
                            }
                        }
                )
            }

            stage('Build') {
                // timeout(20) {
                if (buildConfig.isLibRepo()) {
                    if (jobUtil.getBranchName() == 'master') {
                        echo 'Deploy to Npm Repo'
                        if (!npmUtil.deployNpm(buildConfig)) {
                            throw new Exception("Build Npm Failed!")
                        }
                    }
                } else if (buildConfig.isAppRepo()) {
                    echo 'Build TAR and DockerImage'
                    jobUtil.buildApps(buildConfig)
                }
                // }
            }

            isSuccess = currentBuild.result == null || currentBuild.result == 'SUCCESS'
        } finally {
            if (buildConfig.isLibRepo()) {
                notifyUtil.notifyNpm(buildConfig.getNotifyConfig(), jobUtil.getRepoName(), jobUtil.getBranchName(), isSuccess)
            } else {
                notifyUtil.notifyRepo(buildConfig.getNotifyConfig(), jobUtil.getRepoName(), jobUtil.getBranchName(), isSuccess)
            }
        }
    }
}